﻿// <copyright file="Program.cs" company="Tom Ballard">
// Copyright (c) Tom Ballard.  All rights reserved.
// </copyright>

namespace ExampleTool
{
    #region Using directives
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using ExampleTool.Properties;
    #endregion

    /// <summary>
    /// Example command line tool.
    /// </summary>
    internal static class Program
    {
        /// <summary>
        /// Program entry point.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <returns>The return value.</returns>
        private static int Main(string[] args)
        {
            int result = CommandLine.Run(
            () =>
            {
                var c = new CommandLine(args);
                Func<string, string, IEnumerable<string>> search;
                Action action = null;
                foreach (var i in c.Option)
                {
                    switch (c.List[i])
                    {
                        case "-d":
                        case "--debug":
                            if (!CommandLine.HandleDebug())
                            {
                                c.ShowOptionError(i);
                                goto Help;
                            }

                            break;
                        case "-l":
                        case "--list":
                            if (!object.ReferenceEquals(action, null))
                            {
                                c.ShowOptionError(i);
                                goto Help;
                            }

                            search = Directory.EnumerateFileSystemEntries;
                            action = () =>
                            {
                                var files = CommandLine.Glob(c.File, search);
                                ListFiles(files);
                            };
                            break;

                        case "-t":
                        case "--type":
                            if (c.AddFlag("-t", i + 1, 1))
                            {
                                string flag = c.List[i + 1].ToUpperInvariant();
                                switch (flag)
                                {
                                    case "D":
                                        search = Directory.EnumerateDirectories;
                                        break;

                                    case "F":
                                        search = Directory.EnumerateFiles;
                                        break;

                                    default:
                                        c.ShowOptionError(i + 1);
                                        goto Help;
                                }
                            }
                            else
                            {
                                c.ShowOptionError(i);
                                goto Help;
                            }

                            break;

                        default:
                            c.ShowOptionError(i);
                            goto Help;
                    }
                }

                if (object.ReferenceEquals(action, null))
                {
                    goto Help;
                }

                action();
                return CommandLine.SuccessResult;

            Help:
                CommandLine.ShowHelp(
                (err, exe) =>
                {
                    err.WriteLine("{0} -l|--list [-t|--type d|f] [-d] <files>", exe);
                    err.WriteLine("  List <files>.");
                    err.WriteLine("  -t|--type: d: directories only; f: files only.  Default is all.");
                    err.WriteLine(CommandLineStrings.DebugHelp);
                });
                return CommandLine.ShowHelpResult;
            });
            return result;
        }

        /// <summary>
        /// Lists the files.
        /// </summary>
        /// <param name="files">The files.</param>
        private static void ListFiles(IEnumerable<string> files)
        {
            foreach (string file in files)
            {
                Console.WriteLine("{0}: {1}", file, Path.GetFullPath(file));
            }
        }
    }
}
